package cn.ydw.www.plugin;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import cn.ydw.www.plugin.utils.AssetUtil;
import cn.ydw.www.plugin.utils.Logger;
import cn.ydw.www.plugin_lib.PluginManager;

public class MainActivity extends AppCompatActivity {

    private MainActivity act = this;
    private Uri srcUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        findViewById(R.id.btn_main_load).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (srcUri == null) {
//                    // 打开系统的文件选择器
//                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//                    intent.addCategory(Intent.CATEGORY_OPENABLE);
//                    intent.setType("*/*");
//                    startActivityForResult(intent, 100);
//                } else {
//                   loadApk();
//                }
                loadApk();


            }
        });

        findViewById(R.id.btn_main_jump).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PackageInfo packageInfo = PluginManager.getInstance().getPackageInfo();
                if (packageInfo != null && packageInfo.activities != null && packageInfo.activities.length > 0) {
                    String name = packageInfo.activities[0].name;
                    Logger.e("启动 = " + name);
                    PluginManager.getInstance().gotoActivity(act, name);
                }
            }
        });
    }

    private void loadApk() {
//        File apk = FileUtils.getNewInternalCacheFile(act, "plugin_mudule.apk", "");
//        FileUtils.copyFile(act, srcUri, apk);
//        File apk = new File("file:///storage/emulated/0/Android/data/cn.ydw.www.plugin/files/apk/plugin_mudule.apk");
        try {
//            if (apk.exists()) {
//            }
//            String path = "/storage/emulated/0/Android/data/cn.ydw.www.plugin/files/apk/plugin_mudule.apk";
            String path = AssetUtil.copyAssetToCache(act, "plugin_mudule.apk");
            assert path != null;
            PluginManager.getInstance().loadPluginApk(path);
            Logger.e("加载成功 ----- " + path);

        } catch (Throwable e) {
            Logger.e("load dex error", e);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 100) {
                if (data != null) {
                    srcUri = data.getData();
                    Logger.e("获取的路径 = " + srcUri);
                    loadApk();
                }
            }
        }
    }
}